package com.web.kftsdk.common.api;

import com.web.kftsdk.common.bean.*;
import com.web.kftsdk.common.exception.PayErrorException;
import com.web.kftsdk.common.http.HttpConfigStorage;
import com.web.kftsdk.common.http.HttpRequestTemplate;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * 支付基础服务
 *
 * @author: xukk
 */
public abstract class BasePayService implements PayService {

    protected PayConfigStorage payConfigStorage;

    protected HttpRequestTemplate requestTemplate;

    /**
     * 设置支付配置
     *
     * @param payConfigStorage 支付配置
     */
    @Override
    public BasePayService setPayConfigStorage(PayConfigStorage payConfigStorage) {
        this.payConfigStorage = payConfigStorage;
        return this;
    }

    @Override
    public PayConfigStorage getPayConfigStorage() {
        return payConfigStorage;
    }

    @Override
    public HttpRequestTemplate getHttpRequestTemplate() {
        return requestTemplate;
    }

    /**
     * 设置并创建请求模版， 代理请求配置这里是否合理？？，
     *
     * @param configStorage http请求配置
     * @return 支付服务
     */
    @Override
    public BasePayService setRequestTemplateConfigStorage(HttpConfigStorage configStorage) {
        this.requestTemplate = new HttpRequestTemplate(configStorage);
        return this;
    }


    public BasePayService(PayConfigStorage payConfigStorage) {
        this(payConfigStorage, null);
    }

    public BasePayService(PayConfigStorage payConfigStorage, HttpConfigStorage configStorage) {
        setPayConfigStorage(payConfigStorage);
        setRequestTemplateConfigStorage(configStorage);
    }

    @Override
    public boolean verify(Map<String, String> params) throws PayErrorException {
        return false;
    }

    @Override
    public String sign(String content, String charset) throws PayErrorException {
        return null;
    }

    /**
     * 将请求参数或者请求流转化为 Map
     *
     * @param parameterMap 请求参数
     * @param is           请求流
     * @return 获得回调的请求参数
     */
    @Override
    public Map<String,String> getParameter2Map (Map<String, String[]> parameterMap, InputStream is) throws PayErrorException {
        Map<String, String> params = new TreeMap<String,String>();
        for (Iterator iter = parameterMap.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = parameterMap.get(name);
            String valueStr = "";
            for (int i = 0,len =  values.length; i < len; i++) {
                valueStr += (i == len - 1) ?  values[i] : values[i] + ",";
            }
            if (!valueStr.matches("\\w+")){
                try {
                    if(valueStr.equals(new String(valueStr.getBytes("iso8859-1"), "iso8859-1"))){
                        valueStr=new String(valueStr.getBytes("iso8859-1"), payConfigStorage.getInputCharset());
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            params.put(name, valueStr);
        }
        return params;
    }

    @Override
    public PayOutMessage getPayOutMessage(String code, String message) {
        return null;
    }

    @Override
    public PayOutMessage successPayOutMessage(PayMessage payMessage) {
        return null;
    }

    @Override
    public Map<String, String> getParameter2Map(String str, String transactionType) throws PayErrorException {
        return null;
    }

    @Override
    public PayRes createOrder(PayOrder<?> payOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> createOrder(PayOrder<?> payOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes refund(RefundOrder<?> refundOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> refund(RefundOrder<?> refundOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes close(PayOrder<?> payOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> close(PayOrder<?> payOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes genAppPay(PayOrder<?> payOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> genAppPay(PayOrder<?> payOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes genQrPay(PayOrder<?> payOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> genQrPay(PayOrder<?> payOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes microPay(PayOrder<?> payOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> microPay(PayOrder<?> payOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes query(PayOrder<?> payOrder) {
        return null;
    }

    @Override
    public <T> T query(PayOrder<?> payOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes refundQuery(RefundOrder<?> refundOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> refundQuery(RefundOrder<?> refundOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes downLoadBill(String billDate, String billType, String... params) {
        return null;
    }

    @Override
    public <T> PayRes<T> downLoadBill(String billDate, String billType, Callback<T> callback, String... params) {
        return null;
    }

    @Override
    public PayRes transfer(TransferOrder<?> transferOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> transfer(TransferOrder<?> transferOrder, Callback<T> callback) {
        return null;
    }

    @Override
    public PayRes transferQuery(TransferOrder<?> transferOrder) {
        return null;
    }

    @Override
    public <T> PayRes<T> transferQuery(TransferOrder<?> transferOrder, Callback<T> callback) {
        return null;
    }
}
